package com.yilin.platform.base_core.commons.utils;

import com.google.gson.reflect.TypeToken;
import com.yilin.platform.base_core.application.ReturnData;
import com.yilin.platform.base_core.commons.exception.BusinessException;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * 参数验证工具类
 *
 * @author zhangjinyuan
 * @date 2022/11/7
 */
public class ParamUtil {

    /**
     * 判空
     */
    public static Boolean isNull(Class<?> clazz, String str) {
        if (clazz.isArray()) {
            String[] split = str.split(",");
            return split.length == 0;
        }
        if (List.class.isAssignableFrom(clazz)) {
            List list = JsonUtil.jsonToObject(str, new TypeToken<List>() {
            });
            return CollectionUtils.isEmpty(list);
        } else if (Map.class.isAssignableFrom(clazz)) {
            Map<Object, Object> map = JsonUtil.jsonToObject(str, new TypeToken<Map<Object, Object>>() {
            });
            return CollectionUtils.isEmpty(map);
        } else if (String.class.isAssignableFrom(clazz)) {
            return (null == str || "".equals(((String) str).trim()));
        } else if (clazz.isEnum()) {
            for (Object enumConstant : clazz.getEnumConstants()) {
                if (enumConstant.toString().equals(str)) return false;
            }
            return true;
        } else if (Object.class.isAssignableFrom(clazz)) {
            return Objects.isNull(str);
        }
        return false;
    }

    /**
     * 对象验空
     */
    public static void isNull(@Nullable Object o, String message) {
        if (Objects.isNull(o)) {
            throw new BusinessException(message);
        }
    }

    /**
     * 集合验空
     */
    public static void isNull(@Nullable Collection<?> collection, String message) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new BusinessException(message);
        }
    }

    /**
     * MAP验空
     */
    public static void isNull(@Nullable Map<?, ?> map, String message) {
        if (CollectionUtils.isEmpty(map)) {
            throw new BusinessException(message);
        }
    }

    /**
     * String验空
     */
    public static void isNull(@Nullable String str, String message) {
        if (null == str || "".equals(str.trim())) {
            throw new BusinessException(message);
        }
    }

    /**
     * 字符串判空
     */
    public static void isNull(@Nullable Object[] o, String message) {
        if (o == null || o.length == 0) {
            throw new BusinessException(message);
        }
    }

    /**
     * 对象对比验证
     * 等同于 o!=o1&&o!=o2 throw new BusinessException(message)
     */
    public static void notEquals(String message, Object o, Object... to) {
        int i = 0;
        for (Object o1 : to) {
            if (o != o1) i++;
        }
        if (to.length == i) throw new BusinessException(message);
    }

    /**
     * double对比等同于 o==o1||o==o2 throw new BusinessException(message);
     */
    public static void equals(String message, double o, double to) {
        if (o == to) {
            throw new BusinessException(message);
        }
    }

    /**
     * 对象对比验证
     * 等同于 o==o1||o==o2 throw new BusinessException(message);
     */
    public static void equals(String message, Object o, Object... to) {
        for (Object o1 : to) {
            if (o == o1) {
                throw new BusinessException(message);
            }
        }
    }

    /**
     * 字符串比较
     */
    public static void notEquals(String message, String str, String srt1) {
        if (!str.equals(srt1)) {
            throw new BusinessException(message);
        }
    }

    /**
     * Double对比验证
     * 等同于 o!=o1&&o!=o2 throw new BusinessException(message)
     */
    public static void notEquals(String message, Double o, Double... to) {
        int i = 0;
        for (Double o1 : to) if (!Objects.equals(o, o1)) i++;
        if (to.length == i) throw new BusinessException(message);
    }

    /**
     * 远程调用结果验证
     */
    public static <T> T isSuccess(ReturnData<T> returnData) {
        if (!returnData.isSuccess()) throw new BusinessException(returnData.getMsg());
        return returnData.getData();
    }


}
